'''
This is the admin module where you can edit the main menu of the
admin popup. This is for advanced coders, who know exactly what
they're doing.
'''

import es
import popuplib
import wcs
from wcs import wcs, wcsconfig
wcs_admins = wcsconfig.wcs_admins

players = {}

def load():
	admin_menu = wcs.Admin()
	
	admin_menu.registerMenu('Give XP',GiveXp) 
	# This creates a new option on the main menu, and calls the function GiveXp
	
	admin_menu.registerMenu('Give Levels',GiveLevel)
	
	if not es.exists('clientcommand','WCSADDXP'):
		es.regclientcmd('WCSADDXP','wcs/admin/GiveTheXp') 
		# Makes a command so we can use an escinputbox later
	if not es.exists('clientcommand','WCSADDLEVEL'):
		es.regclientcmd('WCSADDLEVEL','wcs/admin/GiveTheLevel')
	
def player_disconnect(ev):
	'''
	Deletes a player from the dictionary if he has a key, as to save memory and CPU power
	'''
	global players
	if players.has_key(ev['userid']):
		del players[ev['userid']]
	
def IsAdmin(userid):
	if es.getplayersteamid(userid) in wcs_admins: return True
	return False
	
def UpdateTargetList(userid):
	'''
	Deletes the old player menu, updates it with the newer people
	on the server.
	'''
	players = popuplib.construct('TargetMenu', 'players', '#all')
	players.settitle('Chose a player')
	players.menuselectfb = TargetChosen
	players.send(userid)
	
def TargetChosen(userid, choice, popupid):
	'''
	Saves the targets ID to the users dict. This makes it so we
	can call the users ID at a later stage
	'''
	players[userid]['target'] = choice
	function = players[userid]['function']
	if callable(function): 
		function(userid)
	
def GiveXp(userid):
	players[userid] = {'function':SelectXpAmount}
	UpdateTargetList(userid) # Sends an updated version of the playerlist
	
def SelectXpAmount(userid):
	es.escinputbox(30, userid, '=== WarCraft: Source Give Xp ===', 'Enter the XP Value', 'WCSADDXP')
	# This creates an escape box option, where the user will input the amount of xp he wants to
	# give to the user... After he has selected the amount, it'll run the command WCSADDXP, and
	# the value will be passed as an extra argument.. e.g. if the admin inputed 50 for the value,
	# it'd run the command WCSADDXP 50.
	es.tell(userid,'#green','Press escape on your keyboard to input a value for the XP.')
	
def GiveTheXp():
	userid = es.getcmduserid()
	# Gets the userid of the user
	if IsAdmin(userid):
	# If the user is an admin
		amount = int(es.getargv(1)) # Gets the amount of XP the user inputted
		if amount > 0: # I.E, neither negative nor 0
			target = players[userid]['target'] # retrieves the target's info
			player = wcs.Command(target)
			player.GiveXp(amount,' being liked by an administrator.') # Finalizes the command, and adds the xp
		else:
			es.tell(userid,'#green','You must input a value greater than 0!')
		
def GiveLevel(userid):
	players[userid] = {'function':SelectLevelAmount}
	UpdateTargetList(userid)
	
def SelectLevelAmount(userid):
	es.escinputbox(30, userid, '=== WarCraft: Source Give Level ===', 'Enter the Level Value', 'WCSADDLEVEL')
	es.tell(userid,'#green','Press escape on your keyboard to input a value for the Level.')
	
def GiveTheLevel():
	userid = es.getcmduserid()
	if IsAdmin(userid):
		amount = int(es.getargv(1))
		if amount > 0:
			target = players[userid]['target']
			player = wcs.Command(target)
			player.GiveLevel(amount,' being liked by an administrator.')
		else:
			es.tell(userid,'#green','You must input a value greater than 0!')
